package com.hedl.classroom.controller;

import com.hedl.classroom.base.exception.CommonResult;
import com.hedl.classroom.model.dto.UploadFileParamsDto;
import com.hedl.classroom.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * ProjectName: onlinec-classroom
 * DateTime: 2023/7/12 12:10
 * Author: Big Watermelon
 * Description:
 */
@Api(value = "大文件上传接口",tags = "大文件上传接口")
@Slf4j
@RestController
public class BigFileController {

    @Resource
    private MediaFileService mediaFileService;

    @ApiModelProperty("文件上传前检查文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileMd5",value = "md5值",required = true,dataType = "String")
    })
    @PostMapping("/upload/checkfile")
    public CommonResult<Boolean> checkfile(@RequestParam("fileMd5") String fileMd5){
        return mediaFileService.checkFile(fileMd5);
    }

    @ApiModelProperty("分块文件上传前的检测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileMd5",value = "md5值",required = true,dataType = "String"),
            @ApiImplicitParam(name = "chunk",value = "分块值",required = true,dataType = "int")
    })
    @PostMapping("/upload/checkchunk")
    public CommonResult<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                            @RequestParam("chunk") int chunk){
        return mediaFileService.checkChunk(fileMd5,chunk);
    }

    @ApiModelProperty("上传分块文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件值",required = true,dataType = "Object"),
            @ApiImplicitParam(name = "fileMd5",value = "md5值",required = true,dataType = "String"),
            @ApiImplicitParam(name = "chunk",value = "分块值",required = true,dataType = "int")
    })
    @PostMapping("/upload/uploadchunk")
    public CommonResult uploadchunk(@RequestParam("file")MultipartFile file,
                                    @RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("chunk") int chunk) throws Exception{
        return mediaFileService.uploadChunk(fileMd5,chunk,file.getBytes());
    }

    @ApiModelProperty("合并文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileMd5",value = "md5值",required = true,dataType = "String"),
            @ApiImplicitParam(name = "fileName",value = "文件名",required = true,dataType = "String"),
            @ApiImplicitParam(name = "chunkTotal",value = "分块总数",required = true,dataType = "int")
    })
    @PostMapping("/upload/mergechunks")
    public CommonResult mergechunks(@RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("fileName") String fileName,
                                    @RequestParam("chunkTotal") int chunkTotal){
        Long companyId = 1232141425L;
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(fileName);
        uploadFileParamsDto.setTags("课程视频");
        uploadFileParamsDto.setFileType("001002");
        uploadFileParamsDto.setRemark("");
        return mediaFileService.mergeChunks(companyId,fileMd5,chunkTotal,uploadFileParamsDto);
    }
}
